// pages/eva_page/eva_page.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
var cropperObj;
import {
    getCraftsInfo,
    getStoreInfo,
    addOrderEva,
    getEvaInfo
} from '../../static/api/api.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        uploadImg:[],
        remark:'',
        cropperParams: {
            width: 350,
            height: 350,
            src: ''
        },
        isShowCropper: false,
        sp: 0,
        letterCount: 0,
        store_id: '',
        store_user_id: '',
        order_type: '',
        order_id: '',
        eva_type:'',
        goods_id:'',
        evaParams: {
            'door': {
                'speciality_score':{
                    name: '专业',
                    val:0
                },
                'communicate_score':{
                    name: '沟通',
                    val: 0
                }, 
                'punctuality_score':{
                    name: '准时',
                    val: 0
                },
            },
            'shop': {
                'effect_score':{
                    name: '效果',
                    val: 0
                },
                'environment_score':{
                    name: '环境',
                    val: 0
                }, 
                'service_score':{
                    name: '服务',
                    val: 0
                },
            }
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options)
        if (options.order_type == 10) {
            this.setData({
                store_id: '',
                store_user_id: options.store_user_id ,
                order_type: options.order_type,
                order_id: options.order_id,
                eva_type:'door',
                goods_id: options.goods_id
            })
            this.getCraftsInfo()
        } else {
            this.setData({
                store_id: options.store_id,
                store_user_id: '',
                order_type: options.order_type,
                order_id: options.order_id,
                eva_type:'shop',
                goods_id: options.goods_id
            })
            this.getStoreInfo()
        }
        if (options.type == 'edit') {
            this.setData({
                'pageType': 'edit'
            })
            this.getEvaInfo()
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    inputValueChange(e) {
        utils.inputValueChange(e, this)
    },
    scoreChange(e) {
        if(this.data.pageType=='edit') return;
        let dataset = e.currentTarget.dataset;
        let key = 'evaParams.' + this.data.eva_type+'.'+dataset.index+'.val'
        this.setData({
            [key]: dataset.val
        })
    },
    getEvaInfo() {
        let options = {
            id: this.data.order_id
        }
        wx.showLoading({
            title: '加载中...',
        })
        getEvaInfo(options).then(response => {
            wx.hideLoading();
            let res = response.data;
            this.setData({
                'evaParams.door.speciality_score.val': res.speciality_score || 0,
                'evaParams.door.communicate_score.val': res.communicate_score || 0,
                'evaParams.door.punctuality_score.val': res.punctuality_score || 0,
                'evaParams.shop.effect_score.val': res.effect_score || 0,
                'evaParams.shop.environment_score.val': res.environment_score || 0,
                'evaParams.shop.service_score.val': res.service_score || 0,
                'remark':res.remark,
                'uploadImg': utils.formatImgArr(res.other_imgs)
            })
            // this.setData({
            //     mainData: response.data
            // })

        }).catch(response => {
            wx.hideLoading();
        })
    },
    getCraftsInfo() {
        let options = {
            store_user_id: this.data.store_user_id
        }
        wx.showLoading({
            title: '加载中...',
        })
        getCraftsInfo(options).then(response => {
            wx.hideLoading();
            this.setData({
                mainData: response.data
            })

        }).catch(response => {
            wx.hideLoading();
        })
    },
    getStoreInfo() {
        let options = {
            store_id: this.data.store_id
        }
        wx.showLoading({
            title: '加载中...',
        })
        getStoreInfo(options).then(response => {
            wx.hideLoading();
            this.setData({
                mainData: response.data
            })

        }).catch(response => {
            wx.hideLoading();
        })
    },
    addOrderEva() {
        let options;
        if(this.data.order_type == 10){
            options = {
                order_id: this.data.order_id,
                goods_id: this.data.goods_id,
                other_imgs: this.data.uploadImg.join(','),
                remark: this.data.remark,
                store_user_id: this.data.store_user_id,
                store_id: this.data.store_id,
                speciality_score: this.data.evaParams.door.speciality_score.val,
                communicate_score: this.data.evaParams.door.communicate_score.val,
                punctuality_score: this.data.evaParams.door.punctuality_score.val
            }
        }else{
            options = {
                order_id: this.data.order_id,
                goods_id: this.data.goods_id,
                other_imgs: this.data.uploadImg.join(','),
                remark: this.data.remark,
                store_user_id: this.data.store_user_id,
                store_id: this.data.store_id,
                effect_score: this.data.evaParams.shop.effect_score.val,
                service_score: this.data.evaParams.shop.service_score.val,
                environment_score: this.data.evaParams.shop.environment_score.val,
            }
        }
        
        wx.showLoading({
            title: '加载中...',
        })
        addOrderEva(options).then(response => {
            wx.hideLoading();
            setTimeout(function(){
                wx.showToast({
                    icon: 'none',
                    title: '评价成功',
                })
            },200)
            
            setTimeout(function(){
                wx.navigateBack({
                    
                })
            })
        }).catch(response => {
            wx.hideLoading();
        })
    },
    uploadShopOtherImg() {
        let that = this;
        cropperObj = null
        cropperObj = this.selectComponent("#imageCropper");
        cropperObj.upload(function(){
            that.setData({
                'isShowCropper':true,
            })
        },function(){
            that.setData({
                'isShowCropper': false,
            })
        });
        this.loadimage()
        return;

    },
    cropperload(e) {
        console.log("cropper初始化完成");
    },
    loadimage(e) {
        cropperObj.imgReset();
    },
    clipCropperImg() {
        let that = this;
        console.log('裁剪的图片');
        wx.showLoading({
            title: '图片处理中...',
        })
        cropperObj.getImg(function (res) {
            wx.hideLoading()
            that.uploadImg(res.url, function (data) {
                console.log(data)
               
                    that.data.uploadImg.push(data.data.url)
                    let images = that.data.uploadImg
                    console.log(images)
                    // 限制最多只能留下3张照片
                    that.setData({
                        uploadImg: images.length <= 3 ? images : images.slice(0, 3),
                        'isShowCropper': false
                    })

            })
        })
    },
    cancelClipBtn() {
        this.setData({
            'isShowCropper': false,
        })
        this.loadimage()
    },
    //上传图片
    uploadImg(path, cb) {
        let that = this;
        setTimeout(function () {
            var urls = config.interfaceHost + "/api.php/common/Qiniu/upLoadImg";
            console.log(path);
            wx.showLoading({
                title: '图片处理中..',
            })
            wx.uploadFile({
                url: urls,
                filePath: path,
                name: 'image',
                header: {
                    'content-type': 'multipart/form-data',
                    'site': config.site,
                    'os': config.os,
                    'token': wx.getStorageSync('token')
                },
                success: function (res) {
                    wx.hideLoading()
                    var str = res.data;
                    console.log(res.data);
                    if (typeof cb == 'function') {
                        cb(JSON.parse(res.data));
                    }

                },
                fail: function (res) {
                    wx.hideLoading()
                    console.log(res);
                    wx.showModal({
                        title: '提示',
                        content: "网络请求失败，请确保网络是否正常",
                        showCancel: false,
                        success: function (res) {

                        }
                    });
                    wx.hideToast();
                }
            });
        }, 500)
    },
    //删除图片
    delImage(e) {
        let dataset = e.currentTarget.dataset;
        console.log(dataset)
        let images = this.data.uploadImg;
        images.splice(dataset.index, 1)
        this.setData({
            uploadImg: images
        })
    },
    //预览图片
    handleImagePreview(e) {
        const idx = e.target.dataset.index
        const images = this.data.uploadImg
        wx.previewImage({
            current: images[idx], //当前预览的图片
            urls: images, //所有要预览的图片
        })
    },
})